package com.studentmanagement.service;

import com.studentmanagement.dao.TeacherMapper;
import com.studentmanagement.model.Teacher;
import com.studentmanagement.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TeacherService {
    public Teacher getTeacherById(Integer id) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            TeacherMapper mapper = session.getMapper(TeacherMapper.class);
            return mapper.getTeacherById(id);
        }
    }

    public Teacher getTeacherByUserId(Integer userId) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            TeacherMapper mapper = session.getMapper(TeacherMapper.class);
            return mapper.getTeacherByUserId(userId);
        }
    }

    public Teacher getTeacherByTeacherId(String teacherId) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            TeacherMapper mapper = session.getMapper(TeacherMapper.class);
            return mapper.getTeacherByTeacherId(teacherId);
        }
    }

    public List<Teacher> getAllTeachers() {
        try (SqlSession session = MyBatisUtil.openSession()) {
            TeacherMapper mapper = session.getMapper(TeacherMapper.class);
            return mapper.getAllTeachers();
        }
    }

    public List<Teacher> searchTeachers(String name, String department, String title) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            TeacherMapper mapper = session.getMapper(TeacherMapper.class);
            Map<String, Object> params = new HashMap<>();
            if (name != null && !name.trim().isEmpty()) {
                params.put("name", name);
            }
            if (department != null && !department.trim().isEmpty()) {
                params.put("department", department);
            }
            if (title != null && !title.trim().isEmpty()) {
                params.put("title", title);
            }
            return mapper.searchTeachers(params);
        }
    }

    public boolean insertTeacher(Teacher teacher) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            TeacherMapper mapper = session.getMapper(TeacherMapper.class);
            int result = mapper.insertTeacher(teacher);
            session.commit();
            return result > 0;
        }
    }

    public boolean updateTeacher(Teacher teacher) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            TeacherMapper mapper = session.getMapper(TeacherMapper.class);
            int result = mapper.updateTeacher(teacher);
            session.commit();
            return result > 0;
        }
    }

    public boolean deleteTeacher(Integer id) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            TeacherMapper mapper = session.getMapper(TeacherMapper.class);
            int result = mapper.deleteTeacher(id);
            session.commit();
            return result > 0;
        }
    }

    public boolean deleteTeacherByUserId(Integer userId) {
        try (SqlSession session = MyBatisUtil.openSession()) {
            TeacherMapper mapper = session.getMapper(TeacherMapper.class);
            int result = mapper.deleteTeacherByUserId(userId);
            session.commit();
            return result > 0;
        }
    }
} 